//
//  YTPickerView.m
//  YiTuUserClient
//
//  Created by 石少庸 on 2017/3/25.
//  Copyright © 2017年 YiTu. All rights reserved.
//

#import "YTPickerView.h"

@interface YTPickerView ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (strong, nonatomic) UIView *backGroundView;
@property (assign, nonatomic) YTPickerType type;

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *selecteLabel;
@property (strong, nonatomic) UIDatePicker *datePicker;
@end

@implementation YTPickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self userinterfaceLayout];
    }
    return self;
}


+ (instancetype)pickerWithType:(YTPickerType)type {
    YTPickerView *picker = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    picker.type = type;
    picker.dimmedMask = YES;
    return picker;
}

+ (instancetype)pickerWithDataSource:(NSArray<NSString *> *)dataArray {
    YTPickerView *picker = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    picker.type = YTPickerTypeDataSource;
    picker.dimmedMask = YES;
    picker.dataArray = dataArray;
    return picker;
}

- (void)setDataArray:(NSArray<NSString *> *)dataArray {
    _dataArray = dataArray;
    _selecteLabel.text = _dataArray[1];
    [_pickerView reloadAllComponents];
}

- (void)userinterfaceLayout {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    
    _backGroundView = [[NSBundle mainBundle] loadNibNamed:@"YTPickerView" owner:self options:nil].lastObject;
    _backGroundView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 258);
    [window addSubview:_backGroundView];
    
    @weakify(self);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] bk_initWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
        @strongify(self);
        [self dismissWithAction:NO];
    }];
    [self addGestureRecognizer:tap];
}

- (void)dealloc {}

#pragma mark - UIPickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return _dataArray.count;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    _selecteLabel.text = _dataArray[row];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return _dataArray[row];
}

#pragma mark - show dismiss

- (void)show {
    [self showWithType:_type];
    
    if (_dimmedMask) {
        self.backgroundColor = [UIColor blackColor];
        self.alpha = 0.3;
    }
    [UIView animateWithDuration:0.3 animations:^{
        _backGroundView.frame = CGRectMake(0, kScreenHeight - 258, kScreenWidth, 258);
    }];
}

- (void)showWithType:(YTPickerType)type {
    switch (type) {
        case YTPickerTypeDate:
            [self showDatePickerWithMode:UIDatePickerModeDateAndTime];
            break;
        case YTPickerTypeMonthDay:
            [self showDatePickerWithMode:UIDatePickerModeDate];
            break;
        case YTPickerTypeHourMinute:
            [self showDatePickerWithMode:UIDatePickerModeTime];
            break;
        case YTPickerTypeDataSource:
            [_pickerView selectRow:1 inComponent:0 animated:NO];
            break;
    }
}

- (void)showDatePickerWithMode:(UIDatePickerMode)mode {
    [_pickerView removeFromSuperview];
    _selecteLabel.text = self.tips;
    _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, 213)];
    [_backGroundView addSubview:_datePicker];
    _datePicker.datePickerMode = mode;
    _datePicker.minuteInterval = 5;
    if (!_birthday) { // 如果是选生日,那么就可以选以前的时间
        _datePicker.minimumDate = [NSDate date];
    }
}

- (void)dismissWithAction:(BOOL)isFinish {
    [UIView animateWithDuration:0.3 animations:^{
        _backGroundView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 258);
    } completion:^(BOOL finished) {
        if (isFinish) {
            switch (_type) {
                case YTPickerTypeDate:
                case YTPickerTypeMonthDay:
                case YTPickerTypeHourMinute:
                    [self dismissCallBack:[self formatDate]];
                    break;
                case YTPickerTypeDataSource:
                    [self dismissCallBack:_selecteLabel.text];
                    break;
            }
        }
        [_backGroundView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

- (void)dismissCallBack:(NSString *)backStr {
    
    if (_type == YTPickerTypeMonthDay) {
        backStr = [backStr substringToIndex:10];
    }
    
    if (_completion) {
        _completion(backStr);
    }
    
    if ([_delegate respondsToSelector:@selector(pickerViewDidDismiss:)]) {
        [_delegate pickerViewDidDismiss:backStr];
    }
}

- (NSString *)formatDate {
    NSDate *date=[_datePicker date];
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    formatter.dateFormat=@"yyyy-MM-dd HH:mm:ss";
    NSString *timeStr=[formatter stringFromDate:date];
    
    return timeStr;
}

#pragma mark - action

- (IBAction)closeAction:(UIButton *)sender {
    [self dismissWithAction:NO];
}

- (IBAction)selectFinshAction:(UIButton *)sender {
    [self dismissWithAction:YES];
}

@end
